package org.hxzon.configdesigner.web.pages;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.hxzon.configdesigner.core.CfgParser;
import org.hxzon.configdesigner.core.CfgValue;
import org.hxzon.configdesigner.web.pages.base.PageBase;
import org.hxzon.configdesigner.web.util.CfgUtil;

public class AddMapElement extends PageBase {

    @Property
    private CfgValue cfg;
    @Property
    private String childKey;
    //
    @InjectPage
    private View pageView;

    @OnEvent(EventConstants.ACTIVATE)
    void init(int indexCode) {
        cfg = CfgUtil.findCfg(indexCode);
    }

    @OnEvent(EventConstants.SUBMIT)
    Object submit() {
        CfgValue childCfg = CfgParser.buildListElementCfgValue(cfg);
        cfg.addValue(childKey, childCfg);
        CfgUtil.indexCfg(childCfg);
        return pageView.withInit(childCfg.indexCode());
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object toPassivate() {
        return cfg.indexCode();
    }
}
